import storage from '@/libs/storage';
import util from '@/libs/util';

const user = {
    state: {
        tokens: {},
        user: {}
    },
    mutations: {
        logout (state, vm) {
            storage.remove('tokens');
            storage.remove('user');

            state.tokens = {};
            state.user = {};

            // 清除自动更新Token
            clearInterval(window.refreshTokenTimer);
        },

        setTokens (state, tokens) {
            storage.set('tokens', tokens);
            state.tokens = tokens;

            window.axios.defaults.headers.common['Authorization'] = tokens.token_type + ' ' + tokens.access_token;
        },

        setUser (state, user) {
            storage.set('user', user);
            state.user = user;
        }
    },

    actions: {
        setTokens ({ commit }, tokens) {
            commit('setTokens', tokens);
        },

        setUser ({ commit }, user) {
            commit('setUser', user);
        }
    }
};

export default user;
